package xcu.lxj.wxlogin.controller;

import cn.hutool.extra.qrcode.QrCodeUtil;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import xcu.lxj.wxlogin.config.WxLoginProperties;
import xcu.lxj.wxlogin.constant.UrlConstant;
import xcu.lxj.wxlogin.domain.entity.AccessTokenResponse;
import xcu.lxj.wxlogin.domain.entity.WeChatUserInfo;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * @Author XinJian Lei
 * @Date 2024/11/23 19:38
 * @Version 1.0
 */
@RestController
@Slf4j
public class LoginController {

    @Value("${website}")
    String WEBSITE;
    @Autowired
    WxLoginProperties wxLoginProperties;
    @Autowired
    RestTemplate restTemplate;

    /**
     * 提供此接口供wx进行调用
     * @param signature 微信加密签名，signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
     * @param timestamp 时间戳
     * @param nonce 随机数
     * @param echostr 没问题返回 echostr
     * @return
     */
    @GetMapping("/wxCallBack")
    public String wxCallBack(@RequestParam("signature")String signature,
                           @RequestParam("timestamp")String timestamp,
                           @RequestParam("nonce")Integer nonce,
                           @RequestParam("echostr") String echostr){
        log.info("signature = {},timestamp = {},nonce={} , echostr ={}",signature,timestamp,nonce,echostr);
        return echostr;
    }


    /**
     *  登录返回二维码
     * @param response
     * @throws Exception
     */
    @GetMapping("/wxLogin")
    public void login(HttpServletResponse response)throws Exception{
        String redirectUrl = URLEncoder.encode(WEBSITE+"/wxLoginCallBack", StandardCharsets.UTF_8);

        String url = UrlConstant.GET_CODE_URL
                .replace("SCOPE","snsapi_userinfo")
                .replace("APPID", wxLoginProperties.getAppId())
                .replace("REDIRECT_URI",redirectUrl);

        QrCodeUtil.generate(url,300,300, "jpg",response.getOutputStream());

    }

    /**
     *  扫码成功后会进行的回调 去验证code 返回 accessToken 用于进行其他请求
     * @param code
     * @param state
     * @return
     */
    @GetMapping("/wxLoginCallBack")
    public WeChatUserInfo loginCallBack(@RequestParam("code")String code, @RequestParam("state") String state){
        String url = UrlConstant.GET_ACCESS_TOKEN_URL
                .replace("APPID",wxLoginProperties.getAppId())
                .replace("SECRET",wxLoginProperties.getAppSecret())
                .replace("CODE",code);
        AccessTokenResponse accessTokenResponse = restTemplate.getForObject(url, AccessTokenResponse.class);
        log.info("accessTokenResponse = {}",accessTokenResponse);


        // 请求 用户信息
        url = UrlConstant.GET_USERINFO
                .replace("OPENID",accessTokenResponse.getOpenid())
                .replace("ACCESS_TOKEN",accessTokenResponse.getAccessToken());
        WeChatUserInfo userInfo = restTemplate.getForObject(url, WeChatUserInfo.class);
        log.info("userInfo = {}",userInfo);
        return userInfo;
    }





}
